package com.lzm.seckill.server.utils;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Description:随机生成订单编号
 *
 * @Author: Liuzemin
 * @Date: 2020/9/15 14:47
 */
public class RandomUtil {

    private static final SimpleDateFormat dateFormatOne = new SimpleDateFormat("yyyyMMddHHmmssSS");

    private static final ThreadLocalRandom random = ThreadLocalRandom.current();
    /**
     * 方式一：生成订单编号
     * @return
     */
    public static String generateOrderCode() {
        return dateFormatOne.format(DateTime.now().toDate()) + generateCode(4);
    }

    public static String generateCode(final int num){
        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0; i<num; i++){
            int j = random.nextInt(9);
            stringBuffer.append(j);
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateOrderCode());
    }
}
